package org.liquidByte.simpleChat.server;

import java.io.IOException;

import org.liquidByte.simpleChat.communication.ChannelController;

public class ServerController {
	private static ChannelController controller;
	private static boolean listening;
	
	private static ChannelController getController() {
		if (ServerController.controller == null) {
			ServerController.controller = new ChannelController(new RemoteCallHandlerImpl());
			ServerController.listening = false;
		}
		return ServerController.controller;
	}
	private static void setController(ChannelController controller) {
		if (ServerController.controller != null) {
			ServerController.controller.dispose();
			ServerController.listening = false;
		}
		ServerController.controller = controller;
	}
	public static boolean isListening(){
		return ServerController.listening;
	}
	public static void listen(int port) throws IOException {
		// verifica se deve ser realmente feito o listening
		if (!ServerController.listening) {
			ServerController.getController().listen(port);
			ServerController.listening = true;
		}
	}
	public static void stop() {
		ServerController.setController(null);
	}
}
